package cn.itsub.boot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * MVC接收参数时的日期解析
 */
@Configuration
public class DateFormatConfig {

    @Bean
    public Formatter<LocalDateTime> localDateTimeFormatter() {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        return new Formatter<LocalDateTime>() {
            @Override
            public String print(LocalDateTime localDate, Locale locale) {
                return DateTimeFormatter.ofPattern(pattern,locale).format(localDate);
            }

            @Override
            public LocalDateTime parse(String s, Locale locale) {
                if (isNumeric(s)){
                    return LocalDateTime.ofEpochSecond(Long.parseLong(s),0, ZoneOffset.of("+8"));
                }
                return LocalDateTime.parse(s,DateTimeFormatter.ofPattern(pattern,locale));
            }
        };
    }

    @Bean
    public Formatter<LocalDate> localDateFormatter() {
        String pattern = "yyyy-MM-dd";
        return new Formatter<LocalDate>() {
            @Override
            public String print(LocalDate localDate, Locale locale) {
                return DateTimeFormatter.ofPattern(pattern,locale).format(localDate);
            }

            @Override
            public LocalDate parse(String s, Locale locale) {
                return LocalDate.parse(s,DateTimeFormatter.ofPattern(pattern,locale));
            }
        };
    }

    @Bean
    public Formatter<Date> dateFormatter() {

        String[] patterns = new String[]{
                "yyyy-MM-dd HH:mm:ss",
                "yyyy/MM/dd HH:mm:ss",
                "yyyy-MM-dd",
                "HH:mm:ss",
        };

        List<SimpleDateFormat> dflist = new ArrayList<>();
        for (String str : patterns) {
            dflist.add(new SimpleDateFormat(str));
        }

        return new Formatter<Date>() {
            @Override
            public String print(Date date, Locale locale) {
                for (SimpleDateFormat df : dflist) {
                    try {
                        return df.format(date);
                    }catch (Exception e){ }
                }
                throw new RuntimeException("无法识别的日期格式");
            }

            @Override
            public Date parse(String s, Locale locale) throws ParseException {
                for (SimpleDateFormat df : dflist) {
                    try {
                        return df.parse(s);
                    }catch (Exception e){ }
                }
                throw new RuntimeException("无法识别的日期格式");
            }
        };

    }

    //判断字符串是否为数字
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]+");
        return pattern.matcher(str).matches();
    }
}
